package com.pcbly.web.action.publicinfo;

import java.util.Date;

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.publicinfo.ArticleBean;
import com.pcbly.web.business.publicinfo.ArticleService;
import com.pcbly.web.components.hibernate.support.Page;
import com.pcbly.web.components.form.AnalyseCommonDataExt;

/**
 * @author carl.song Copyright (c) 2010 Lightsoft, International. All rights
 *         reserved.
 * 
 */

public class ArticleAction extends BaseAction<ArticleBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1279571573681577167L;

	protected ArticleService articleService;

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	private ArticleBean article;

	private String orderBy;

	private Page<ArticleBean> pages;

	private boolean asc = true;

	public boolean isAsc() {
		return asc;
	}

	public void setAsc(boolean asc) {
		this.asc = asc;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public ArticleBean getArticle() {
		return article;
	}

	public Page<ArticleBean> getPages() {
		return pages;
	}

	public void setPages(Page<ArticleBean> pages) {
		this.pages = pages;
	}

	public void setArticle(ArticleBean article) {
		this.article = article;
	}

	public String view() throws Exception {
		long startTime = System.currentTimeMillis();
		article = articleService.getArticleBeanById(getId());
		extendCustomer = articleService.findArticleBeanExtView(
				"t_publicinfo_article_ext", getId());

		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.view();
	}

	public String update() throws Exception {
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		article = articleService.getArticleBeanById(getId());
		extendCustomer = articleService.findArticleBeanExtEdit(
				"t_publicinfo_article_ext", getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.update();
	}

	public String index() throws Exception {
		long startTime = System.currentTimeMillis();

		long start = getPage();
		start = start * getPageSize();

		if (orderBy == null || "".equals(orderBy)) {
			orderBy = " id ";
		}
		pages = new Page<ArticleBean>();
		pages.setStart(start);
		pages.setPageSize(getPageSize());
		pages = articleService.findArticleBeanByCond(article, pages, orderBy,
				asc);
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("page:::::" + getPage());
			log.debug("pageSize:::::" + getPageSize());
			log.debug("time ms:::::" + startTime);

			System.out.println("page:::::" + getPage());
			System.out.println("pageSize:::::" + getPageSize());
			System.out.println("time ms:::::" + startTime);
		}
		return super.index();
	}

	public String add() throws Exception {
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		article = new ArticleBean();
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		extendCustomer = articleService
				.findArticleBeanExtCreate("t_publicinfo_article_ext");
		return super.add();
	}

	public String save() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(article.getId());
		}
		AnalyseCommonDataExt ext = new AnalyseCommonDataExt();
		ext.setTableName("t_publicinfo_article_ext");
		ext.setFeedbackKeyAndValue(feedbackKeyAndValue);

		try {
			if (article.getId() == null || "".equals(article.getId().trim())) {// new
				article.setCreateDate(new Date());
				article.setCreateUserId(getUserId());
				article.setRecycled("0");
				article = articleService.saveArticleBean(article);
				ext.setId(article.getId());
				String sql = ext.execute();
				if (!(sql == null || "".equals(sql.trim()))) {
					articleService.saveArticleBeanExt(sql);
				}
				setMyMessage(getText("entity.saved"));
			} else {// update
				articleService.updateArticleBean(article);

				boolean iflag = articleService.getHasArticleBeanExtData(
						"t_publicinfo_article_ext", article.getId());
				ext.setHaveData(iflag);
				ext.setId(article.getId());
				String sql = ext.execute();
				if (!(sql == null || "".equals(sql.trim()))) {
					articleService.saveArticleBeanExt(sql);
				}

				setMyMessage(getText("entity.updated"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		extendCustomer = articleService.findArticleBeanExtEdit(
				"t_publicinfo_article_ext", article.getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.save();
	}

	public String dwrSave(ArticleBean bean) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(bean.getId());
		}
		try {
			if (bean.getId() == null || "".equals(bean.getId())) {// new
				articleService.saveArticleBean(bean);
			} else {// update
				articleService.updateArticleBean(bean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseSave(bean.getId());
	}

	public String delete() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(getId());
		}
		if (getId().indexOf(",") >= 0) {
			articleService.removeArticleBeanByIds(getId());
		} else {
			articleService.removeArticleBeanById(getId());
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		article = null;
		return super.delete();
	}

	public String dwrDelete(String id) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(id);
		}
		if (id.indexOf(",") >= 0) {
			articleService.removeArticleBeanByIds(id);
		} else {
			articleService.removeArticleBeanById(id);
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseDelete(id);
	}

}